package org.restoran.model;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.restoran.service.parser.jackson.CategoryBeanDeserializer;
import org.restoran.service.parser.jackson.IdentityJsonSerialyzer;

import javax.persistence.*;
import javax.persistence.Table;
import java.util.*;

/**
 * @author Snisar Bogdan.
 */
@Entity
@Table(name = "ITEM")
public class Item extends NameEntity {

    public Item() {}

    public Item(Integer id) {
        super(id);
    }

    public Item(Integer id, String name, String description, Category category, Double price) {
        super(id, name);
        this.description = description;
        this.category = category;
        this.price = price;
    }

    @Column(name = "DESKRIPTION", nullable = true)
    private String description;

    @JsonSerialize(using = IdentityJsonSerialyzer.class)
    @JsonDeserialize(using = CategoryBeanDeserializer.class)
    @JsonProperty("categoryId")
    @ManyToOne
    @JoinColumn(name = "CATEGORY_ID")
    private Category category;

    @Column(name = "PRICE")
    private Double price;

    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.item",
            cascade=CascadeType.ALL,
            orphanRemoval = true)
    public Set<ItemOrder> orders = new HashSet<>(0);


    public Set<ItemOrder> getOrders() {
        return orders;
    }

    public void setOrders(Set<ItemOrder> orders) {
        this.orders = orders;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Item{" + "id=" + getId() +
                ", description='" + description + '\'' +
                ", category=" + category +
                ", price=" + price +
                " };";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Item)) return false;

        Item item = (Item) o;

        if (category != null ? !category.equals(item.category) : item.category != null) return false;
        if (description != null ? !description.equals(item.description) : item.description != null) return false;
        if (price != null ? !price.equals(item.price) : item.price != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = description != null ? description.hashCode() : 0;
        result = 31 * result + (category != null ? category.hashCode() : 0);
        result = 31 * result + (price != null ? price.hashCode() : 0);
        return result;
    }
}
